const router = require('express').Router({

});
const { check, validationResult } = require('express-validator'); //数据验证
const classControllers = require("../../controllers/classControllers");
/* GET */
router.get(
  '/',
  [
    check('pageNo').notEmpty().withMessage('页码不能为空'),
    check('pageSize').notEmpty().withMessage('条数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }
    let data = await classControllers
                    .selectList(
                      req.query.pageNo,
                      req.query.pageSize,
                      req.query.cname,
                      req.query.major,
                      req.query.department
                    );

    res.sendResult(data,200,"请求成功");
    
  }
);

router.post(
  '/',
  [
    check('class_major_id').notEmpty().withMessage('class_major_id参数不能为空'),
    check('class_grade').notEmpty().withMessage('class_grade参数不能为空'),
    check('class_name').notEmpty().withMessage('class_name参数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data1 = await classControllers.selectByNameAndCondition(req.body.class_name,null,"0");
    
    if(data1){
      res.sendResult(null, 400, "添加失败,班级名已存在");
      return;
    }
    let data2 = await classControllers.selectByNameAndCondition(req.body.class_name,null,"1");

    if(data2){
      await classControllers.updateByDid(data2.class_id,req.body.class_major_id,req.body.class_grade,req.body.class_name,0)
    }else{
      await classControllers.insert(req.body.class_major_id,req.body.class_grade,req.body.class_name)
    }
    res.sendResult({}, 201, "添加成功");
  }
);

router.put(
  '/',
  [
    check('class_id').notEmpty().withMessage('class_id参数不能为空'),
    check('class_major_id').notEmpty().withMessage('class_major_id参数不能为空'),
    check('class_grade').notEmpty().withMessage('class_grade参数不能为空'),
    check('class_name').notEmpty().withMessage('class_name参数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data1 = await classControllers.selectById(req.body.class_id);
    
    if(!data1){
      res.sendResult(null, 400, "更新失败,class_id不存在");
      return;
    }

    let data2 = await classControllers.selectByNameAndCondition(req.body.class_name,req.body.class_id);
    if(data2){
      res.sendResult(null, 400, "更新失败,班级名已存在");
      return;
    }

    let flag = await classControllers.updateByDid(req.body.class_id,
                                                  req.body.class_major_id,
                                                  req.body.class_grade,
                                                  req.body.class_name
                                                );
    res.sendResult({}, 202, flag?"更新成功":"数据已更新");
  }
);

router.delete(
  '/',
  [
    check('class_id').notEmpty().withMessage('class_id参数不能为空'),
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data = await classControllers.selectById(req.query.class_id);
    if(!data){
      res.sendResult(null, 400, "删除失败,class_id不存在");
      return;
    }

    let flag = await classControllers.deleteByDid(req.query.class_id);
    res.sendResult({}, 203, flag?"删除成功":"数据已删除");
  }
);

module.exports = router;
